package com.twotiger.and.activity.user;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.util.DigestUtils;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.view.GesturePassWordView;

public class LockActivity extends BaseActivity implements View.OnClickListener {
    public FragmentManager subFragmentManager;
    private GesturePassWordView mGesturePassWordView;
    private Button forget_psd_bt;
    private Button cut_account_bt;
    private TextView gesture_hint_tv;
    private int deblock_count = 5;
    private TextView welcome_text;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.gesture_password_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        mGesturePassWordView = (GesturePassWordView) view.findViewById(R.id.mGesturePassWordView);
        gesture_hint_tv = (TextView) view.findViewById(R.id.gesture_hint_tv);
        forget_psd_bt = (Button) view.findViewById(R.id.forget_psd_bt);
        cut_account_bt = (Button) view.findViewById(R.id.cut_account_bt);
        welcome_text = (TextView) view.findViewById(R.id.welcome_text);
        
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
        case R.id.forget_psd_bt:
            application.logout();
            intent = new Intent(this, LoginPage.class).putExtra("FROM", R.id.forget_psd_bt);
            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
            break;
        case R.id.cut_account_bt:
            // 去登录
            application.logout();
            intent = new Intent(LockActivity.this, LoginPage.class).putExtra("FROM",
                R.id.cut_account_bt);
            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void setListener() {
        forget_psd_bt.setOnClickListener(this);
        cut_account_bt.setOnClickListener(this);
    }

    @Override
    protected void processLogic() {
        welcome_text.setText("欢迎您\n" + StringUtils.hideMidString(application.getUser().phone, 3, 4));
        mGesturePassWordView.setOnCompleteListener(new GesturePassWordView.OnCompleteListener() {
            @Override
            public void onComplete(String mPassword) {
                if (DigestUtils.md5(mPassword).equals(application.getUser().gesturePwd)) {
                    mGesturePassWordView.clearPassword();
                    gesture_hint_tv.setText("验证成功");
                    application.vaildGesturePwd = true;
                    finish();
                    overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
//                    if (application.hasMoreActivities()) {
//                        finish();
//                        overridePendingTransition(R.anim.silent_anim, R.anim.slide_out_to_bottom);
//                    } else {
//                        Intent intent = new Intent(LockActivity.this, HomeActivity.class);
//                        if (getIntent().getExtras() != null) {
//                            intent.putExtra("PUSH", getIntent().getSerializableExtra("PUSH"));
//                        }
//                        intent.putExtra(Constants.pageIndex, 3);
//                        intent.putExtra(Constants.login_state, false);
//                        startNewActivity(intent, R.anim.fade_in_anim, R.anim.slide_out_to_bottom,
//                            true);
//                    }
                } else {
                    errorDetail();
                }
            }

            @Override
            public void onShortComplete(String password) {
                System.out.println(password + "----"
                                   + PreferencesUtils.getString(mContext, Constants.gesture_psd));
                errorDetail();
            }
        });
    }

    private void errorDetail() {
        if (deblock_count < 6 && deblock_count > 1) {
            deblock_count--;
            gesture_hint_tv.setText("密码错误，您还可以再输入" + deblock_count + "次");
            gesture_hint_tv.setTextColor(LockActivity.this.getResources()
                .getColor(R.color.tran_red));
        } else {
            //退出登录
            application.logout();
            PromptManager.showConfirmAlertCommon(mContext, 0, "",
                "您输入的错误次数过多，请用密码重新登录", "确定", "", false, new ReChargeClickListener() {

                    @Override
                    public void onClickConfirm() {
                        mGesturePassWordView.resetPassWord(null);
                        // 去登录
                        Intent intent = new Intent(LockActivity.this, LoginPage.class);
                        if (getIntent().getExtras() != null) {
                            intent.putExtra("PUSH", getIntent().getSerializableExtra("PUSH"));
                        }
                        startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
                    }

                    @Override
                    public void onClickCancel() {

                    }
                });
        }
        mGesturePassWordView.clearPassword();
    }


    @Override
    protected void onBackPress() {
        exitApp(); //按返回键退出应用！！！
    }
    

    long mExitTime = 0;

//    @Override
//    public boolean dispatchKeyEvent(KeyEvent event) {
//        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
//            return true;
//        }
//
//        return super.dispatchKeyEvent(event);
//    }

}
